package leetcode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * ClassName: MeituanT2
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/6 10:33
 * @Version 1.0
 */
public class MeituanT2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] parts = br.readLine().split(" ");
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(parts[i]);
        }

        // 计算MEX M
        boolean[] present = new boolean[n + 2];
        for (int num : a) {
            if (num >= 0 && num <= n + 1) {
                present[num] = true;
            }
        }
        int M = 0;
        while (M < present.length && present[M]) {
            M++;
        }

        // 计算最大和
        long sum = (long) M * (M + 1) / 2 + (long) M * (n - M);
        System.out.println(sum);

        // 构建频率映射
        Map<Integer, Integer> freqMap = new HashMap<>();
        for (int num : a) {
            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
        }

        // 生成重排方案：先输出0到M-1各一次
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < M; i++) {
            result.add(i);
            freqMap.put(i, freqMap.get(i) - 1);
        }

        // 输出剩余数字
        for (int num : freqMap.keySet()) {
            int count = freqMap.get(num);
            for (int j = 0; j < count; j++) {
                result.add(num);
            }
        }

        // 格式化输出
        StringBuilder sb = new StringBuilder();
        for (int num : result) {
            sb.append(num).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}
